package org.alex.samples.web;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.alex.samples.model.ExamScore;
import org.alex.samples.model.Subject;
import org.alex.samples.model.User;
import org.alex.samples.service.IExamScoreService;
import org.alex.samples.service.ISubjectService;
import org.alex.samples.service.IUserService;
import org.alex.samples.vo.CommonMessageResultVO;
import org.alex.samples.vo.StudentScoreVO;
import org.alex.samples.vo.SubjectVO;
import org.alex.samples.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TeacharController {
	@Autowired
	IExamScoreService scoreService;
	
	@Autowired
	IUserService userService;
	
	@Autowired
	ISubjectService subjectService;
	
	@RequestMapping("/score")
	public String checkRole(HttpSession session,Model model) throws Exception{
		UserVO uservo = (UserVO) session.getAttribute("user");
		if(uservo.getRole() == 2 || uservo.getRole() == 1){
			List<ExamScore> scoreList = scoreService.getAllScore();
			List<StudentScoreVO> scorevoList = new ArrayList<>();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
			for(ExamScore examScore:scoreList){
				if(examScore.getScore() != null){
					StudentScoreVO scorevo = new StudentScoreVO();
					scorevo.setId(examScore.getId());
					scorevo.setTestDate(sdf.format(examScore.getExamTime()));
					scorevo.setTestScore(examScore.getScore());
					
					User user = userService.getUserById(examScore.getUserId());
					scorevo.setStudentId(user.getId());
					scorevo.setStudentName(user.getName());
					
					SubjectVO subjcevo = subjectService.FindSubjectById(examScore.getSubjectId());
					scorevo.setSubjectName(subjcevo.getName());
					scorevo.setId(examScore.getId());
					scorevoList.add(scorevo);
				}
			}
			
			List<SubjectVO> subjectList = new ArrayList<>();
			List<Subject> subjcetList = subjectService.FindAllSubject();
			for(Subject s:subjcetList){
				SubjectVO svo = new SubjectVO();
				svo.setName(s.getName());
				svo.setId(s.getId());
				subjectList.add(svo);
			}
			
			model.addAttribute("scorevoList", scorevoList);
			model.addAttribute("subjectList", subjectList);
			return "subject/score";
		}
		return null;
	}
	
	
	@RequestMapping(value = "searchScore")
	@ResponseBody
	public List<StudentScoreVO> searchScore(StudentScoreVO scorevo) throws Exception{
		List<StudentScoreVO> rs = new ArrayList<>();
		List<ExamScore> list= scoreService.searchScore(scorevo.getSubjectId(), scorevo.getStudentId(), scorevo.getStudentName(), scorevo.getTestDate());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
		for(ExamScore e : list){
			StudentScoreVO svo = new StudentScoreVO();
			svo.setId(e.getId());
			svo.setStudentId(e.getUserId());
			svo.setStudentName(userService.getUserById(e.getUserId()).getName());
			svo.setSubjectId(e.getSubjectId());
			svo.setSubjectName(subjectService.FindSubjectById(e.getSubjectId()).getName());
			svo.setTestDate(sdf.format(e.getExamTime()));
			svo.setTestScore(e.getScore());
			rs.add(svo);
		}
		return rs;
	}
	
	@RequestMapping(value = "showEditScore")
	@ResponseBody
	public StudentScoreVO showEditScore(StudentScoreVO svo) throws Exception{
		StudentScoreVO rs = new StudentScoreVO();
		ExamScore s = scoreService.getScoreById(svo.getId());
		rs.setId(s.getId());
		rs.setStudentId(s.getSubjectId());
		rs.setStudentName(userService.getUserById(s.getUserId()).getName());
		rs.setSubjectId(s.getSubjectId());
		rs.setSubjectName(subjectService.FindSubjectById(s.getSubjectId()).getName());
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
		rs.setTestDate(sdf.format(s.getExamTime()));
		
		rs.setTestScore(s.getScore());
		return rs;
	}
	
	@RequestMapping(value = "saveEditScore")
	@ResponseBody
	public CommonMessageResultVO saveEditScore(StudentScoreVO svo){
		CommonMessageResultVO rs = new CommonMessageResultVO();
		rs.setSuccess(false);
		try{
			ExamScore e = scoreService.getScoreById(svo.getId());
			e.setScore(svo.getTestScore());
			scoreService.updateScore(e);
			rs.setSuccess(true);
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		return rs;
	}
	
	@RequestMapping(value = "deleteScore")
	@ResponseBody
	public CommonMessageResultVO deleteScore(StudentScoreVO svo){
		CommonMessageResultVO rs = new CommonMessageResultVO();
		rs.setSuccess(false);
		try{
			ExamScore s = new ExamScore();
			s.setId(svo.getId());
			scoreService.deleteScore(s);
			rs.setSuccess(true);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return rs;
	}
	
}
